var host = 'http://www.teach.com/index.php/Home/';

/**
 * 表单提交
 * */
$('.ajax-post').click(function () {
    //加载层
    var index = layer.load(2, {shade: [0.2, '#000']});

    var formData = $('form').serialize();
    $.post(url, formData, function (res) {
        if (res.result == 1) {
            setTimeout(function () {
                window.location.href = res.url;
            }, res.timer)
        } else if (res.result == 2) {
            layer.close(index);
            layer.msg(res.msg);
            if (res.token) $("[name='__token__']").val(res.token);
        }
    }, 'json');

    setTimeout(function () {
        layer.close(index);
    }, 3000)
    return false;
});

function post_ajax(url, data, callback) {
    var url = host + url;

    $.post(url, data, function (res) {
        $.hideLoading();
        callback(res)
    }, 'json')
}

function layer_notice(data) {
    console.log(data);
    if (data) {
        if (data.result == 2) {
            layer.msg(data.msg);
        }
    }

}

/****************** 触底加载更多 ******************/
var page = 1;
var pagesize = 10;
var pageLoading = false;//分页标记状态

/**
* 分页初始化
* */
function initLoad()
{
    page = 1;
    pageLoading = false;
}

/**
* 加载更多
* */
function load(url, param = {}, callback)
{
    $(window).scroll(
        function () {
            var scrollTop = $(this).scrollTop();
            var scrollHeight = $(document).height();
            var windowHeight = $(this).height();
            if (scrollTop > scrollHeight - windowHeight - 5) {
                if(!pageLoading){
                    pageLoading = true;
                    $.showLoading();

                    param.page = page + 1;
                    param.pagesize = pagesize;

                    post_ajax(url, param, function (res){
                        callback(res);
                    })
                }
            }
        }
    );
}
/****************** 触底加载更多 ******************/